const parameter = getApp().globalData.parameter
const domainName = getApp().globalData.domainName

const util = require("../../../../utils/util")
const oss_domain = getApp().globalData.oss_domain
const license_list = getApp().globalData.license_list
const pilot_rank_list = getApp().globalData.pilot_rank_list
const uav_type_list = getApp().globalData.uav_type_list
const uav_level_list = getApp().globalData.uav_level_list
const status_list = getApp().globalData.status_list
const status_bgColors = getApp().globalData.status_bgColors


// pages/apply_join_class/apply_join_class.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    status_bgColors,
    status_list,
    license_list,
    pilot_rank_list,
    uav_type_list,
    uav_level_list,
    icon_url:oss_domain + 'static/picture/app_icon/',
    tomorrow:util.daysLaterOrAfter(new Date, 1),
  },
  
set_jg(e){
  getApp().get_vibration()

    let url_data = {
      stu_id:this.stu_id,
      stu_name:this.data.user_info.real_name,

      jg_stu_id:this.data.stu_info.jg_stu_id,
      jg_id:this.data.stu_info.jg_id,
      jg_name:this.data.stu_info.jg_name,
      photo:this.data.stu_info.photo,
    }
    wx.navigateTo({
      url: '/pages/students/stu_register/set_stuTojg/set_stuTojg?url_data=' + encodeURIComponent(JSON.stringify(url_data)),
    })
    this.can_onShow = true
  },
      

edit_name(){
  getApp().get_vibration()
  if(!this.data.stu_info.jg_stu_id){
    wx.showToast({
      title: '先绑定机构',
      icon: 'error',
    })
    return
  }
  let url_data = {
    stu_id:this.stu_id,
    photo:this.data.stu_info.photo,
    stu_name:this.data.user_info.real_name,
  }
  wx.navigateTo({
    url: '/pages/users/set_user_name/set_user_name?url_data=' + encodeURIComponent(JSON.stringify(url_data)),
  })
  this.can_onShow = true
},


edit_idNo(){
  getApp().get_vibration()
  if(!this.data.stu_info.jg_stu_id){
    wx.showToast({
      title: '先绑定机构',
      icon: 'error',
    })
    return
  }
  let url_data = {
    stu_id:this.stu_id,
    photo:this.data.stu_info.photo,
    stu_name:this.data.user_info.real_name,
    id_no:this.data.user_info.id_no,
  }
  wx.navigateTo({
    url: '/pages/users/set_user_idNo/set_user_idNo?url_data=' + encodeURIComponent(JSON.stringify(url_data)),
  })
  this.can_onShow = true
},

update_pilotRank(){
  getApp().get_vibration()
  if(!this.data.stu_info.jg_stu_id){
    wx.showToast({
      title: '先绑定机构',
      icon: 'error',
    })
    return
  }
  let url_data = {
    jg_stu_id:this.data.stu_info.jg_stu_id,
    photo:this.data.stu_info.photo,
    stu_name:this.data.user_info.real_name,
    pilot_rank_id:this.data.stu_info.pilot_rank_id,
    jg_id:this.data.stu_info.jg_id,
    page_index_2:this.page_index_1,
  }
  wx.navigateTo({
    url: '/pages/students/stu_register/update_pilotRank/update_pilotRank?url_data=' + encodeURIComponent(JSON.stringify(url_data)),
  })
  this.can_onShow = true
},

update_uav_level(){
  getApp().get_vibration()
  if(!this.data.stu_info.jg_stu_id){
    wx.showToast({
      title: '先绑定机构',
      icon: 'error',
    })
    return
  }
  let url_data = {
    jg_stu_id:this.data.stu_info.jg_stu_id,
    photo:this.data.stu_info.photo,
    stu_name:this.data.user_info.real_name,
    uav_level_id:this.data.stu_info.uav_level_id,
    jg_id:this.data.stu_info.jg_id
  }
  wx.navigateTo({
    url: '/pages/students/stu_register/update_uav_level/update_uav_level?url_data=' + encodeURIComponent(JSON.stringify(url_data)),
  })
  this.can_onShow = true
},

update_uav_type(){
  getApp().get_vibration()
  if(!this.data.stu_info.jg_stu_id){
    wx.showToast({
      title: '先绑定机构',
      icon: 'error',
    })
    return
  }
  let url_data = {
    jg_stu_id:this.data.stu_info.jg_stu_id,
    photo:this.data.stu_info.photo,
    stu_name:this.data.user_info.real_name,
    uav_type_id:this.data.stu_info.uav_type_id,
    jg_id:this.data.stu_info.jg_id
  }
  wx.navigateTo({
    url: '/pages/students/stu_register/update_uav_class/update_uav_class?url_data=' + encodeURIComponent(JSON.stringify(url_data)),
  })
  this.can_onShow = true
},

update_license(){
  getApp().get_vibration()
  if(!this.data.stu_info.jg_stu_id){
    wx.showToast({
      title: '先绑定机构',
      icon: 'error',
    })
    return
  }
  let url_data = {
    jg_stu_id:this.data.stu_info.jg_stu_id,
    photo:this.data.stu_info.photo,
    stu_name:this.data.user_info.real_name,
    license_id:this.data.stu_info.license_id,
    jg_id:this.data.stu_info.jg_id
  }
  wx.navigateTo({
    url: '/pages/students/stu_register/update_stu_license/update_stu_license?url_data=' + encodeURIComponent(JSON.stringify(url_data)),
  })
  this.can_onShow = true
},

update_email(e){
  getApp().get_vibration()
  if(!this.data.stu_info.jg_stu_id){
    wx.showToast({
      title: '先绑定机构',
      icon: 'error',
    })
    return
  }
  let url_data = {
    jg_stu_id:this.data.stu_info.jg_stu_id,
    photo:this.data.stu_info.photo,
    stu_name:this.data.user_info.real_name,
    email:this.data.stu_info.email,
    jg_id:this.data.stu_info.jg_id
  }
  wx.navigateTo({
    url: '/pages/students/stu_register/update_stuEmail/update_stuEmail?url_data=' + encodeURIComponent(JSON.stringify(url_data)),
  })
  this.can_onShow = true
},


set_ins(e){
  getApp().get_vibration()
  if(!this.data.stu_info.jg_stu_id){
    wx.showToast({
      title: '先绑定机构',
      icon: 'error',
    })
    return
  }
  let url_data = {
    jg_stu_id:this.data.stu_info.jg_stu_id,
    photo:this.data.stu_info.photo,
    stu_name:this.data.user_info.real_name,
    ins_id:this.data.stu_info.ins_id,
    jg_id:this.data.stu_info.jg_id
  }
  wx.navigateTo({
    url: '/pages/students/stu_register/bind_insToStu/bind_insToStu?url_data=' + encodeURIComponent(JSON.stringify(url_data)),
  })
  this.can_onShow = true
},


// 设置学员手机号
edit_phone(e){
  let phone = this.data.user_info.phone
  wx.navigateTo({
    url: '/pages/users/set_user_phone/set_user_phone?user_id=' + this.stu_id + '&phone=' + phone,
  })
  this.can_onShow = true
  },

// 设置学员标签
set_label(e){
  getApp().get_vibration()
  if(!this.data.stu_info.jg_stu_id){
    wx.showToast({
      title: '先绑定机构',
      icon: 'error',
    })
    return
  }
  let url_data = {
    jg_stu_id:this.data.stu_info.jg_stu_id,
    photo:this.data.stu_info.photo,
    stu_name:this.data.user_info.real_name,
    label_id:this.data.stu_info.label_id,
    jg_id:this.data.stu_info.jg_id,
  }
  wx.navigateTo({
    url: '/pages/students/stu_register/set_stu_label/set_stu_label?url_data=' + encodeURIComponent(JSON.stringify(url_data)),
  })
  this.can_onShow = true
  },
    
update_addr(){
  getApp().get_vibration()
  if(!this.data.stu_info.jg_stu_id){
    wx.showToast({
      title: '先绑定机构',
      icon: 'error',
    })
    return
  }
let url_data = {
  jg_stu_id:this.data.stu_info.jg_stu_id,
  photo:this.data.stu_info.photo,
  stu_name:this.data.user_info.real_name,
  address:this.data.stu_info.address
}
wx.navigateTo({
  url: '/pages/students/stu_register/update_stu_addr/update_stu_addr?url_data=' + encodeURIComponent(JSON.stringify(url_data)),
})
this.can_onShow = true
},


// 设置学员民族
edit_nation(e){
  getApp().get_vibration()
  if(!this.data.stu_info.jg_stu_id){
    wx.showToast({
      title: '先绑定机构',
      icon: 'error',
    })
    return
  }
  let url_data = {
    jg_stu_id:this.data.stu_info.jg_stu_id,
    photo:this.data.stu_info.photo,
    stu_name:this.data.user_info.real_name,
    nation:this.data.user_info.nation,
  }
  wx.navigateTo({
    url: '/pages/students/stu_register/set_stu_nation/set_stu_nation?url_data=' + encodeURIComponent(JSON.stringify(url_data)),
  })
  this.can_onShow = true
},
    


// 设置学员性别
edit_gender(e){
  getApp().get_vibration()
  let url_data = {
    photo:this.data.stu_info.photo,
    stu_name:this.data.user_info.real_name,
    gender:this.data.user_info.gender,
    stu_id:this.stu_id,
  }
  wx.navigateTo({
    url: '/pages/students/stu_register/edit_gender/edit_gender?url_data=' + encodeURIComponent(JSON.stringify(url_data)),
  })
  this.can_onShow = true
},

// 设置学员状态
set_stu_status(e){
  getApp().get_vibration()
  if(!this.data.stu_info.jg_stu_id){
    wx.showToast({
      title: '先绑定机构',
      icon: 'error',
    })
    return
  }

  let url_data = {
    jg_stu_id:this.data.stu_info.jg_stu_id,
    photo:this.data.stu_info.photo,
    stu_name:this.data.user_info.real_name,
    status_id:this.data.stu_info.status,
    page_index_2:this.page_index_1,
  }
  wx.navigateTo({
    url: '/pages/students/stu_register/set_stu_status/set_stu_status?url_data=' + encodeURIComponent(JSON.stringify(url_data)),
  })
  this.can_onShow = true
},



// 查看图片
view_photo(){
  getApp().get_vibration()

  let photo = this.data.stu_info.photo
  wx.previewImage({
    urls: [photo],
    showmenu: true,
  })
},



//同意/拒绝
review(e){
  getApp().get_vibration()

  let status = e.detail.value
  wx.showModal({
    content: '确定此操作吗？',
    showCancel: true,
    title: status==3?'同意进入班级':status==2?'拒绝！':'',
    success: res => {
      if(res.confirm){
        api.update_stu_status(this,this.data.stu_info.jg_stu_id, status)
      }else{
        this.setData({
          stu_info:this.data.stu_info
        })
      }
    },
  })
},



// 返回
back(){
  getApp().get_vibration();
  wx.navigateBack()
},

  
update_addr_area(){
  getApp().get_vibration()
  if(!this.data.stu_info.jg_stu_id){
    wx.showToast({
      title: '先绑定机构',
      icon: 'error',
    })
    return
  }
  let url_data = {
    jg_stu_id:this.data.stu_info.jg_stu_id,
    photo:this.data.stu_info.photo,
    stu_name:this.data.user_info.real_name,
    province:this.data.stu_info.province,
    city:this.data.stu_info.city,
    area:this.data.stu_info.area,
    jg_id:this.data.stu_info.jg_id
  }
  wx.navigateTo({
    url: '/pages/students/stu_register/update_stu_addr_area/update_stu_addr_area?url_data=' + encodeURIComponent(JSON.stringify(url_data)),
  })
  this.can_onShow = true
},



    // 重新上传头像
reupload(e){
  getApp().get_vibration()
  if(!this.data.stu_info.jg_stu_id){
    wx.showToast({
      title: '先绑定机构',
      icon: 'error',
    })
    return
  }
  wx.chooseMedia({
    count: 1,
    mediaType: ['image'],
    sizeType: ['compressed'],
    success: res => {
      let tempFile = res.tempFiles[0]
      if(tempFile.size>500000){
        wx.showModal({
          content: '上传照片不可超过500KB',
          showCancel: false,
          title: '提示',
        })
        return
      }
      wx.showLoading({
        title: '上传中……',
      })
      wx.uploadFile({
        filePath: tempFile.tempFilePath,
        name: 'files',
        url: domainName + '/stuRegister_info_updateStuPhoto',
        formData: {
          jg_stu_id:this.data.stu_info.jg_stu_id,
        },
        header : {'content-type':'application/x-www-form-urlencoded'},
        success: res => {
          let data = JSON.parse(res.data)
          console.log('data',data)
          if(data.update_res){
            wx.hideLoading()
            wx.showToast({
              title: '更新完成',
            })

            this.setData({
              tempFilePath:tempFile.tempFilePath
            })

            // 更新上一页数据
            if(this.page_index_1){
              var pages = getCurrentPages()
              let page_1 = pages[pages.length-2]  // 上页
              if(page_1){
                let stu_list_1 = page_1.data.stu_list
                stu_list_1[this.page_index_1].photo = tempFile.tempFilePath
                page_1.setData({
                  stu_list:stu_list_1,
                })
              }
            }

          }else{
            wx.showToast({
              title: '更新失败',
              duration: 1500,
              icon: 'error',
            })
          }
        },
      })
    },
  })
},


  


// 更新学员状态
update_stu_status(jg_stu_id,set_status){
  wx.request({
    url: domainName + 'stuRegister_info_updateStuStatus',
    data: {
      jg_stu_id:jg_stu_id,
      set_status:set_status,
    },...parameter,
    success: res => {
      if(res.data.update_res){
        wx.showToast({
          title: '操作成功',
          duration: 1500,
          icon: 'success',
        })
        this.setData({
          stu_info:res.data.stu_info
        })
      }
    },
  })
},


page_init(user_id){
  wx.showLoading()
  wx.request({
    url: domainName + '/register_stu_init',
    data: {user_id},...parameter,
    success: res => {
      // console.log('res',res.data)
      wx.hideLoading()
      this.setData({
        stu_info:res.data.stu_info,
        show_inputPic:res.data.show_inputPic,
        user_info:res.data.user_info,
      })
    },
  })
},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log('options',options)
    this.stu_id = options.stu_id
    this.page_init(this.stu_id)
  },

  onHide(){
  },

  onShow(){
    if(this.can_onShow){
      console.log('onShow')
      this.page_init(this.stu_id)
      this.can_onShow = false
    }
  },

})